import { observable, action, autorun, toJS } from 'mobx';
import { WeaTableNew, WeaForm } from 'comsMobx';

const { TableStore } = WeaTableNew;
import { ListStore } from './listStore';
import { WeaTools } from 'ecCom';
import { message, Modal } from 'antd';
import * as Apis from '../apis/sellChance';
import * as API_CONTACT from '../apis/contact';
import * as API_MINECUSTOMER from '../apis/mineCustomer';
import { CardManageStore } from './cardManageStore';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

export class SellChanceStore extends ListStore {
	@observable title = '';
    @observable rightMenu =[];
    @observable searchBaseParams = {
      labelid: 'my',
    }
    @observable tabDatas = [];
    @observable fromType = {
      fromType: 'sellchance',
    };
    @observable sectorTreeData =[];
    @observable statusOptions = [];
    @observable hrmList = [];
    @observable sellChanceStatus = [];
    @observable isShowResource = false;
    @observable remindCount = 0;
    @observable tagDatas = [];
    @observable istorefresh = 0;
    @observable showRight = false;
    @observable selectValue = '1';
    @observable rightTabData = []; // 右边栏tab
    @observable viewTypeList = [];
    @observable quickList = [];
    @observable quickConditions = {};
    @observable contactOptions = [];
    @observable rightTabKey ='1';
    @observable cardloading = false;

    @observable dateSelect = {
      datatype: '1',
    }
    @observable contactLogs = [];
    @observable contactLogsTotal = 0;
    @observable contacterCard = [];
    @observable contacterCardTotal = 0;
    @observable leaveMessage = [];
    @observable sellChanceCard = [];

    @observable rightInfos = {};
    @observable cardManageStore = new CardManageStore();
    @observable canCreate=false;
    @observable searchsBaseKey='';

    @action
    initDatas=(params) => {
      Apis.getSellChanceCondition(params).then((data) => {
        if (data.status == 'success') {
          this.condition = data.datas;
          this.form && !this.form.isFormInit && this.form.initFormFields(data.datas);
          this.searchsBaseKey = data.searchsBaseKey;
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }
    sellChanceTabs=(params) => {
      Apis.getSellChanceTab(params).then((data) => {
        if (data.status == 'success') {
          this.tabDatas = data.datas;
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }
    doSearch=(params = {}) => {
      this.loading = true;
      // 获取快捷搜索的参数值
      const searchParamsQuick = toJS(this.quickConditions);
      // 获取高级搜索的参数值
      const searchParamsAd = this.form.getFormParams();
      const newParams = { ...this.searchBaseParams, ...searchParamsAd, ...searchParamsQuick, ...params };
      this.searchBaseParams = { ...this.searchBaseParams, ...params };
      Apis.getSellChanceList(newParams).then((data) => {
        if (data.status == 'success') {
          this.tableStore.getDatas(data.sessionkey, params.current || 1);
          this.dataKey = data.sessionkey;
          this.showRight = false;
          this.loading = false;
          this.fromType = { ...this.fromType, crmIds: data.crmIds };
          if(params&&params.haveContacts){
            //页面初始化时，已经搜索了联系，不需要额外加载，其他情况比如搜索了客户，则要过滤对应的联系
          }else{
            this.setRightTabKey('1', false);
            this.getContactLogs({ ...this.searchBaseParams, ...params, crmIds: data.crmIds });
          }
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }
    // 商机状态
    sellChanceStatusAndHrm=(params) => {
      Apis.getSellChanceStatusAndHrm(params).then((data) => {
        if (data.status == 'success') {
          this.sellChanceStatus = data.datas.statusList;
          this.hrmList = data.datas.hrmList || [];
          this.viewTypeList = data.datas.viewTypeList || [];
          this.quickList = data.quickList || [];
          this.contactOptions = data.contactOptions || [];
          this.canCreate = data.canCreate;
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }
    // 获取商机管理  自定义标签列表
    getSellChanceTagsList=(params) => {
      API_MINECUSTOMER.getMyCustomerTagList(params).then((data) => {
        if (data.status == 'success') {
          this.tagDatas = data.datas;
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }
    getRemindCount=(params) => {
      API_CONTACT.getRemindCount(params).then((data) => {
        if (data.status == 'success') {
          this.remindCount = data.datas;
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }
    // 商机管理 -- 是否标记重要
    isMarkToImportant=(params) => {
      API_MINECUSTOMER.markImportant(params).then((data) => {
        if (data.status == 'success') {
          window._table.reLoad();
          return true;
        }
        message.error(getLabel(83437, '出现错误！') + data.msgcode);
      });
    }
    // 商机管理 -- 设置自定义标签
    markToCustomTagType=(params) => {
      API_MINECUSTOMER.markLabel(params).then((data) => {
        if (data.status == 'success') {
          message.success(getLabel('384136', '操作成功!'));
          window._table.reLoad();
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }
    // 标签保存
    crmTagEdit=(params) => {
      API_MINECUSTOMER.crmTagEdit(params).then((data) => {
        if (data.status == 'success') {
          message.success(getLabel('384136', '操作成功!'));
          window._table.reLoad();
          this.getSellChanceTagsList({ type: 'sellchance' });
          this.sellChanceTabs();
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }
    // 标签删除
    crmTagDelete = (params) => {
      API_MINECUSTOMER.crmTagDelete(params).then((data) => {
        if (data.status == 'success') {
          message.success(getLabel('384136', '操作成功!'));
          window._table.reLoad();
          this.getSellChanceTagsList({ type: 'sellchance' });
          this.sellChanceTabs();
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }
    // 人员加载保存
    saveHrmList=(data) => {
      this.hrmList = data;
    }
    updateParams=() => {
      this.searchBaseParams = {
        labelid: this.searchBaseParams.labelid,
      };
      this.istorefresh = this.istorefresh + 1;
      this.selectValue = '1';
      this.rightTabKey = '1';
      this.dateSelect = {
        datatype: '1',
      };
    }
    // 右侧tab切换
    setRightTabKey=(key, bool = true) => {
      this.rightTabKey = key;
      if (key == 1) {
        if (bool) {
          this.getContactLogs({ datatype: this.dateSelect.datatype });
        }
      }
      if (key == 2) {
        this.getContacterCard({ datatype: this.dateSelect.datatype });
      }
    }

    getContactLogs=(params) => {
      this.cardloading = true;
      const newParams = { ...this.searchBaseParams, ...this.fromType, ...this.dateSelect, ...params };
      API_MINECUSTOMER.getContactLogs(newParams).then((data) => {
        this.cardloading = false;
        this.contactLogs = data.datas.rsList;
        this.contactLogsTotal = data.datas.totalSize;
        this.dateSelect = { ...this.dateSelect, datatype: newParams.datatype };
      });
    }
    getContacterCard=(params) => {
      this.cardloading = true;
      const newParams = { ...this.searchBaseParams, ...this.fromType, ...this.dateSelect, ...params };
      API_MINECUSTOMER.getContacterCard(newParams).then((data) => {
        this.cardloading = false;
        this.contacterCard = data.datas.rsList;
        this.contacterCardTotal = data.datas.totalSize;
        this.dateSelect = { ...this.dateSelect, datatype: newParams.datatype };
      });
    }
    @action
    getSellChanceDetail=(id) => {
      this.loading = true;
      Apis.getSellChanceDetail({ sellchanceId: id }).then((data) => {
        this.getSellChanceDetailCallBack(data);
      });
    }
    @action
    getSellChanceDetailCallBack(data) {
      this.loading = false;
      if (data.status == 'success') {
        this.rightInfos = data.datas;
        this.showRight = true;
      } else {
        message.error(getLabel(83437, '出现错误！') + data.msgcode);
      }
      this.cardManageStore.setTabKey('1');
    }
    saveSelectValue=() => {
      this.dateSelect = {
        datatype: '1',
      };
    }

    clearStatus=() => {
      this.rightMenu = [];
      this.searchBaseParams = {
        labelid: 'my',
      };
      this.fromType = {
        fromType: 'sellchance',
      };
      this.isShowResource = false;
      this.remindCount = 0;
      this.showRight = false;
      this.selectValue = '1';
      this.rightTabKey = '1';
      this.dateSelect = {
        datatype: '1',
      };
      this.istorefresh = this.istorefresh + 1;
      this.quickList = [];
      this.quickConditions = {};
      this.resetForm();
    }

    @action
    doSearchQuick = (params, quickMap) => {
      const quickSign = quickMap.quickSign || '';
      const quickSearch = quickMap.quickSearch;
      if (quickSign == 'preyield') {
        const quickValue = params[quickSearch];
        if (quickValue == '0') {
          this.quickConditions[quickSearch] = 0;
          this.quickConditions[`${quickSearch}_1`] = 50000;
        } else if (quickValue == '1') {
          this.quickConditions[quickSearch] = 50000;
          this.quickConditions[`${quickSearch}_1`] = 100000;
        } else if (quickValue == '2') {
          this.quickConditions[quickSearch] = 100000;
          this.quickConditions[`${quickSearch}_1`] = 200000;
        } else if (quickValue == '3') {
          this.quickConditions[quickSearch] = 200000;
          this.quickConditions[`${quickSearch}_1`] = 500000;
        } else if (quickValue == '4') {
          this.quickConditions[quickSearch] = 500000;
          this.quickConditions[`${quickSearch}_1`] = 1000000;
        } else if (quickValue == '5') {
          this.quickConditions[quickSearch] = 1000000;
          this.quickConditions[`${quickSearch}_1`] = '';
        } else {
          this.quickConditions[quickSearch] = '';
          this.quickConditions[`${quickSearch}_1`] = '';
        }
      } else if (quickSign == 'probability') {
        const quickValue = params[quickSearch];
        if (quickValue == '0') {
          this.quickConditions[quickSearch] = 0;
          this.quickConditions[`${quickSearch}_1`] = 0.3;
        } else if (quickValue == '1') {
          this.quickConditions[quickSearch] = 0.3;
          this.quickConditions[`${quickSearch}_1`] = 0.5;
        } else if (quickValue == '2') {
          this.quickConditions[quickSearch] = 0.5;
          this.quickConditions[`${quickSearch}_1`] = 0.7;
        } else if (quickValue == '3') {
          this.quickConditions[quickSearch] = 0.7;
          this.quickConditions[`${quickSearch}_1`] = 0.9;
        } else if (quickValue == '4') {
          this.quickConditions[quickSearch] = 0.9;
          this.quickConditions[`${quickSearch}_1`] = '';
        } else {
          this.quickConditions[quickSearch] = '';
          this.quickConditions[`${quickSearch}_1`] = '';
        }
      } else {
        this.quickConditions[quickSearch] = params[quickSearch];
      }
      this.quickConditions[`${quickSearch}span`] = params[`${quickSearch}span`];
      this.doSearch();
    }

    @action
    showRightChange(v) {
      this.showRight = v;
    }
}
